package com.fernfx.xingtan.contacts.ui;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.fernfx.xingtan.Constant;
import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.main.entity.ContactsInfoEntity;
import com.fernfx.xingtan.user.AccountManager;
import com.fernfx.xingtan.utils.GlideUtil;
import com.fernfx.xingtan.utils.OtherUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import io.rong.imkit.RongIM;
import io.rong.imlib.model.CSCustomServiceInfo;
import io.rong.imlib.model.Conversation;

/**
 * Created by admin on 2018/8/2.
 * 用户详情
 */

public class UserDetailsActivity extends BaseActivity implements View.OnClickListener {

    public static final String RECORDS_BEAN_KEY = "records_bean";

    @BindView(R.id.user_logo_ibn)
    ImageButton userLogoIbn;

    @BindView(R.id.user_name_tv)
    TextView userNameTv;

    @BindView(R.id.user_id_tv)
    TextView userIdTv;

    @BindView(R.id.user_nickname_tv)
    TextView userNickNameTv;

    private ContactsInfoEntity.ObjBean.RecordsBean recordsBean;

    public static void start(Context context, ContactsInfoEntity.ObjBean.RecordsBean recordsBean) {
        Intent intent = new Intent(context, UserDetailsActivity.class);
        intent.putExtra(RECORDS_BEAN_KEY, recordsBean);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_user_details;
    }

    @Override
    protected void initView() {
        setToolbarTitle(getString(R.string.detailed_info));
    }

    @Override
    protected void initData() {
        Intent dataIntent = getIntent();
        if (dataIntent.hasExtra(RECORDS_BEAN_KEY) && dataIntent.getSerializableExtra(RECORDS_BEAN_KEY) instanceof ContactsInfoEntity.ObjBean.RecordsBean) {
            recordsBean = (ContactsInfoEntity.ObjBean.RecordsBean) dataIntent.getSerializableExtra(RECORDS_BEAN_KEY);
            //设置显示内容
            GlideUtil.loadImage(mContext, OtherUtil.checkEmptyDefault(recordsBean.getHeadImg()), userLogoIbn);
            //姓名
            userNameTv.setText(OtherUtil.checkEmptyDefault(recordsBean.getNotes()));
            //昵称
            String ftNodets = String.format(userNickNameTv.getText().toString(), OtherUtil.checkEmptyDefault(String.valueOf(recordsBean.getNickname())));
            userNickNameTv.setText(ftNodets);
            //id
            String ftId = String.format(userIdTv.getText().toString(), OtherUtil.checkEmptyDefault(String.valueOf(recordsBean.getFriendId())));
            userIdTv.setText(ftId);
        }
    }

    @OnClick({R.id.send_msg_btn,R.id.setting_remark_rlt})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_msg_btn:
                if (RongIM.getInstance() != null) {
                    if (null == recordsBean) {
                        return;
                    }
                    String chatName = "";
                    if (null != recordsBean && !TextUtils.isEmpty(recordsBean.getNotes())) {
                        chatName = recordsBean.getNotes();
                    } else if (null != recordsBean && !TextUtils.isEmpty(recordsBean.getNickname())) {
                        chatName = recordsBean.getNickname();
                    }
                    //携带消息并跳转到指定的页面
                    RongIM.getInstance().setMessageAttachedUserInfo(true);
                    RongIM.getInstance().startPrivateChat(this, "xingtan-" + recordsBean.getFriendId(), chatName);
                }
                break;
            case R.id.setting_remark_rlt:
                SettingRemarksActivity.start(mContext);
                break;
        }
    }
}
